﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CJJCH.Web.Manage
{
    public partial class UrlList :PowerBase
    {
        CJJCH.BLL.T_NET_UrlType bll_urltype = new CJJCH.BLL.T_NET_UrlType();
        CJJCH.BLL.T_NET_URL bll_url = new CJJCH.BLL.T_NET_URL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
                InitRequest(); 
                ReapterDataBinding();
            }
        }

        private void InitRequest()
        {
            if (Request.QueryString["keytype"]!=null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["keytype"].ToString().Trim()))
            {
                this.lblKeyType.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["keytype"].Trim());
                this.dropUrlType.SelectedValue = this.lblKeyType.Text;
            }
            else
            {
                this.lblKeyType.Text = "0";
                this.dropUrlType.SelectedValue = "0";
            }
        }

        private void InitControl()
        {
            IList<CJJCH.Model.T_NET_UrlType> list = new List<CJJCH.Model.T_NET_UrlType>();
            list = bll_urltype.GetUrlTypeList();
            for (int i = 0; i <list.Count; i++)
            {
                dropUrlType.Items.Add(new ListItem(list[i].TypeName,list[i].ID.ToString()));
            }
            list = null;
        }

        private void ReapterDataBinding()
        {
            int typeid =Convert.ToInt32(this.lblKeyType.Text.Trim());
            IList<CJJCH.Model.T_NET_URL> list = new List<CJJCH.Model.T_NET_URL>();
            list = bll_url.GetUrlList(typeid);
            rptUrl.DataSource = list;
            rptUrl.DataBind();
            if (list.Count == 0)
                litMessage.Text = "<table width=100%><tr style=height:18px bgcolor=#FFFFFF><td align='center'><font color=red>对不起，系统没有检索出有关信息！<font></td></tr></table>";
            else
                litMessage.Text = "";
        }
        protected void rptUrl_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                ((Image)e.Item.FindControl("ImgPicUrl")).ImageUrl = ((CJJCH.Model.T_NET_URL)e.Item.DataItem).PicUrl == "" ? "../Image/default.jpg" : ((CJJCH.Model.T_NET_URL)e.Item.DataItem).PicUrl;
                ((Label)e.Item.FindControl("lblURLName")).Text = ((CJJCH.Model.T_NET_URL)e.Item.DataItem).URLName;
                ((Label)e.Item.FindControl("lblTypeName")).Text = ((CJJCH.Model.T_NET_URL)e.Item.DataItem).UrlType.TypeName;
                ((Label)e.Item.FindControl("lblURLString")).Text = ((CJJCH.Model.T_NET_URL)e.Item.DataItem).URLString;
                ((LinkButton)e.Item.FindControl("lbtnEdit")).CommandArgument = ((CJJCH.Model.T_NET_URL)e.Item.DataItem).ID.ToString();
                ((LinkButton)e.Item.FindControl("lbtnDel")).CommandArgument = ((CJJCH.Model.T_NET_URL)e.Item.DataItem).ID.ToString();
                ((LinkButton)e.Item.FindControl("lbtnDel")).Attributes.Add("onclick", "javascript:if(confirm('确定要删除该数据吗？')){return true;}else{return false;}"); 
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e) 
        {
            this.lblKeyType.Text = LTP.Common.StringClass.EncodeString(this.dropUrlType.SelectedValue);
            ReapterDataBinding();
        }

        protected void lbtnEdit_Click(object sender, EventArgs e)
        {
            Response.Redirect("UrlEdit.aspx?keytype="+LTP.Common.StringClass.EncodeUrl(this.lblKeyType.Text.Trim())
                +"&ID="+LTP.Common.StringClass.EncodeUrl(((LinkButton)sender).CommandArgument));
        }

        protected void lbtbDel_Click(object sender, EventArgs e)
        {
            if (LTP.Common.CheckInput.CheckGUID(((LinkButton)sender).CommandArgument))
            {
                string res = bll_url.DelUrl(new Guid(((LinkButton)sender).CommandArgument));
                if (res=="")
                {
                    Response.Redirect("UrlList.aspx?keytype=" + LTP.Common.StringClass.EncodeUrl(this.lblKeyType.Text.Trim()));
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack(res);
                }
            }
            else
            {
                LTP.Common.WindowClass.WindowBack("操作错误！");
            }
        }
    }
}